using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DBSourceToolsLib.SysObjects.UI.Forms
{
    public partial class UserOptionsForm : Form
    {
        private string m_strDatabaseName;
        private string m_strUserOptionsXmlFile;
        //private DataSet m_UserOptionsDS;

        public UserOptionsForm(string strDatabaseName, string strUserOptionsXmlFile)
        {
            InitializeComponent();
            this.m_strDatabaseName = strDatabaseName;
            this.m_strUserOptionsXmlFile = strUserOptionsXmlFile;

            if (System.IO.File.Exists(this.m_strUserOptionsXmlFile))
            {
                this.m_scriptOptionsDS.ReadXml(this.m_strUserOptionsXmlFile);
                this.m_scriptOptionsDS.ScriptOptions.AcceptChanges();
            }

            this.m_dbSourceListView.DataSet = this.m_scriptOptionsDS;
            this.m_dbSourceListView.AddColumn("TableName", DBSourceToolsLib.SysObjects.UI.DBSourceListViewColType.Text);
            this.m_dbSourceListView.AddColumn("ScriptTable", DBSourceToolsLib.SysObjects.UI.DBSourceListViewColType.Checkbox);
            this.m_dbSourceListView.Bind();

        }

        private void m_toolStripButtonReload_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;

            SourceDatabase oSourceDatabase
                = DBSourceToolsLib.UserSettings.Instance().getSourceDatabase(m_strDatabaseName);

            DBSourceToolsLib.ScriptOptionsLoader oLoader = new ScriptOptionsLoader(this, 
                DBSourceToolsLib.Commands.DatabaseCommandTypes.LoadUserList);

            oLoader.Execute(ref oSourceDatabase, null);
//            oDBLoadDb.Execute(ref oSourceDatabase, false);

            this.m_scriptOptionsDS = oLoader.ProcScriptOptionsDS;
            this.m_dbSourceListView.DataSet = this.m_scriptOptionsDS;
            this.m_dbSourceListView.Bind();
            //            this.m_scriptOptionsDS.ScriptOptions

            this.Cursor = Cursors.Default;

        }

        private void UserOptionsForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.m_scriptOptionsDS.HasChanges())
            {
                if (MessageBox.Show(this, "Save Changes ?", "View Options", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    FileUtils.EnsureFileIsWriteable(this.m_strUserOptionsXmlFile);
                    this.m_scriptOptionsDS.WriteXml(this.m_strUserOptionsXmlFile);
                }
            }

        }
    }
}